package com.cherrypt.api.core.util;

import com.cherrypt.api.common.enums.LogEnum;
import com.cherrypt.api.model.dataobject.Log;
import com.cherrypt.api.core.service.LogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author intent
 * @date 2019/7/22 22:26
 * @about <link href='http://zzyitj.xyz/'/>
 */
public class LogUtils {
    private static final Logger logger = LoggerFactory.getLogger(LogUtils.class);
    private static ExecutorService executorService = Executors.newFixedThreadPool(2);

    public static void writeLog(LogService logService, Log log) {
        writeLog(logService, log.getUid(), log.getEvent(), log.getDescription(), log.getIp());
    }

    public static void writeLog(LogService logService, int uid, LogEnum logEnum, String description, HttpServletRequest request) {
        writeLog(logService, uid, logEnum, description, IPUtils.getIpAddr(request));
    }

    public static void writeLog(LogService logService, int uid, LogEnum logEnum, String description, String ip) {
        writeLog(logService, uid, logEnum.getEvent(), description, ip);
    }

    public static void writeLog(LogService logService, int uid, String event, String description, String ip) {
        executorService.execute(() -> {
            Log log = new Log();
            log.setUid(uid);
            log.setEvent(event);
            log.setDescription(description);
            log.setIp(ip);
            logService.saveLog(log);
        });
    }
}
